package org.xs.web.http.servlet;

import java.util.Collection;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletRequest;

public class ServletRequestMap implements Map<String, Object>{
	private ServletRequest servletRequest;
	
	public ServletRequestMap(ServletRequest request){
		this.servletRequest = request;
	}
	@Override
	public void clear() {
		throw new UnsupportedOperationException();
	}

	@Override
	public boolean containsKey(Object key) {
		return servletRequest.getAttribute((String)key) != null;
	}

	@Override
	public boolean containsValue(Object value) {
		throw new UnsupportedOperationException();
	}

	@Override
	public Set<java.util.Map.Entry<String, Object>> entrySet() {
		throw new UnsupportedOperationException();
	}

	@Override
	public Object get(Object key) {
		return servletRequest.getAttribute((String)key);
	}

	@Override
	public boolean isEmpty() {
		throw new UnsupportedOperationException();
	}

	@Override
	public Set<String> keySet() { 
		HashSet<String> keys = new HashSet<String>();
		Enumeration<String> elements = servletRequest.getAttributeNames();
		while(elements.hasMoreElements()){
			keys.add(elements.nextElement());
		}
		return keys;
	}

	@Override
	public Object put(String key, Object value) {
		Object result = servletRequest.getAttribute((String)key);
		servletRequest.setAttribute((String)key, value);
		return result;
	}

	@Override
	public void putAll(Map<? extends String, ? extends Object> m) {
		throw new UnsupportedOperationException();
	}

	@Override
	public Object remove(Object key) {
		Object result = servletRequest.getAttribute((String)key);
		servletRequest.removeAttribute((String)key);
		return result;
	}

	@Override
	public int size() {
		throw new UnsupportedOperationException();
	}

	@Override
	public Collection<Object> values() {
		throw new UnsupportedOperationException();
	}

	public ServletRequest getServletRequest() {
		return servletRequest;
	}

}
